package compilator4urbi.actions;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import behaviourdiagram.Mission;

import compilator4urbi.actions.mapping.Mapping;
import compilator4urbi.util.BehaviourDiagramSerializer;


public class GenerateWifiBotMappingTemplate implements IObjectActionDelegate {

	
	
	private IFile file;

	private Mission mission;
	
	public GenerateWifiBotMappingTemplate() {
		
	}

	@Override
	public void run(IAction action) {
		if (file == null)
			return;
		mission = BehaviourDiagramSerializer.load(new File(file.getLocation().toOSString()));
		Mapping mapping = new Mapping(mission, file.getLocation().toOSString());
		mapping.readAndCreateFileWifibot();
		try {
			file.getParent().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
		} catch (CoreException e) {
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection == null){
			return;
		}
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection)selection;
			Object obj = sel.getFirstElement();
			if (obj instanceof IFile) {
				file = (IFile)obj;
			}
		}
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		
	}

}
